package com.chattingroom.mqcosumer.handler;

import com.chattingroom.mqcosumer.service.WebSocketServer;
import com.rabbitmq.ChattingRoom.util.common.enums.FixedQueueEnum;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * 监听消费者消息处理
 */
@Component
public class MessageConsumerHandler implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        System.out.println(message.toString());
        System.err.println("body" + new String(message.getBody(), "utf-8"));
        System.err.println("queueName" + message.getMessageProperties().getConsumerQueue());
        // 向指定的聊天室推送消息
        WebSocketServer.sendInfo(new String(message.getBody(), "utf-8"), message.getMessageProperties().getConsumerQueue());

        // 通知用户哪个聊天室来了未读消息，同一个websocket通过指定 sid, monitorrooms 表示监听通知哪个聊天室来了未读消息，提醒用户
        WebSocketServer.sendInfo(new String(message.getBody(), "utf-8"), FixedQueueEnum.MONITORROOMS.getQueueName());

        if (message.getMessageProperties().getConsumerQueue().equals(FixedQueueEnum.TELLQUITCHATTINGROOM.getQueueName())) {
            // 通知用户已被移出聊天室
            WebSocketServer.sendInfo(new String(message.getBody(), "utf-8"), FixedQueueEnum.TELLQUITCHATTINGROOM.getQueueName());
        }
    }

}
